/*
 * 1. sets i18n.locale and state.locale if possible
 * 2. redirects if not with locale
 */
import {setLocalStorage, LANGKEY} from '@/utils/auth.js';

export default function ({
    isHMR, app, store, route, params, error, redirect, req
  }) {
    if (isHMR) { // ignore if called from hot module replacement
      return;
    } // if url does not have language, redirect to english
    // else if (!params.lang) {
    //   return redirect('/en'+route.fullPath);
    // }
    // based on directory structure _lang/xxxx, en/about has params.lang as "en"
    // console.log("中间件", localStorage.getItem('user'));
    const locale = params.lang || 'en';
    setLocalStorage(LANGKEY, locale, app);
    store.commit('SET_LANG', locale); // set store
    app.i18n.locale = store.state.locale;
    // if (!route.matched.length) {
    //   redirect('/404')
    // }

    // 判断客户端
    let userAgent = req ? req.headers['user-agent'] : navigator.userAgent || ''
    store.commit("SET_IS_MOBILE", app.$tools.isMobile(userAgent, true));
  }